package top.cdmrx.demo.edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.models.auth.In;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import top.cdmrx.demo.edu.entity.Chapter;
import top.cdmrx.demo.edu.entity.Video;
import top.cdmrx.demo.edu.entity.chapter.ChapterVo;
import top.cdmrx.demo.edu.entity.chapter.VideoVo;
import top.cdmrx.demo.edu.mapper.ChapterMapper;
import top.cdmrx.demo.edu.service.ChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import top.cdmrx.demo.edu.service.VideoService;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-09-18
 */
@Service
public class ChapterServiceImpl extends ServiceImpl<ChapterMapper, Chapter> implements ChapterService {

    @Autowired
    private VideoService videoService;


    @Override
    public List<ChapterVo> getChapterVideo(String courseId) {
        //1、查询对应课程号的章节集合
        QueryWrapper<Chapter> chapterQueryWrapper=new QueryWrapper<>();
        chapterQueryWrapper.eq("course_id",courseId);
        List<Chapter> list1 = baseMapper.selectList(chapterQueryWrapper);

        //2、查询对应课程号的小节集合
        QueryWrapper<Video> videoQueryWrapper=new QueryWrapper<>();
        videoQueryWrapper.eq("course_id",courseId);
        List<Video> list2 = videoService.list(videoQueryWrapper);

        List<ChapterVo> finalList=new ArrayList<>();
        //3、封装章节对象
        for (int i = 0; i <list1.size(); i++) {
            Chapter chapter = list1.get(i);
            ChapterVo chapterVo = new ChapterVo();
            BeanUtils.copyProperties(chapter,chapterVo);
            finalList.add(chapterVo);


            //4、封装小节集合
            List<VideoVo> videoVoList=new ArrayList<>();
            for (int j = 0; j <list2.size(); j++) {
                Video video = list2.get(j);
                if (video.getChapterId().equals(chapter.getId())){
                    VideoVo videoVo = new VideoVo();
                    BeanUtils.copyProperties(video,videoVo);
                    videoVoList.add(videoVo);
                }

            }

            chapterVo.setChildren(videoVoList);
        }




        return finalList;
    }

    //删除章节的方法
    @Override
    public boolean deleteChapter(String chapterId) {

        QueryWrapper<Video> videoQueryWrapper = new QueryWrapper<>();
        videoQueryWrapper.eq("chapter_id",chapterId);
        int count = videoService.count(videoQueryWrapper);
        //判断
        if (count>0){
            throw new RuntimeException("删除章节出现错误");
        }else {
            int i = baseMapper.deleteById(chapterId);
            return i>0;
        }

    }

    @Override
    public void removeId(String id) {
        QueryWrapper<Chapter> chapterQueryWrapper = new QueryWrapper<>();
        chapterQueryWrapper.eq("course_id",id);
        baseMapper.delete(chapterQueryWrapper);
    }

}
